using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using IsometricEngine;
using Fated.Scenario.Events;

namespace Fated.Scenario {
    internal class ChoiceScreen : Renderable {
        private int index = 0;
        private int currentLine = 0;
        private bool moreBelow;

        private Choice.Option[] choices;
        private Renderable selector;
        private Choice data;

        public ChoiceScreen(Choice input) : base() {
            data = input;
            choices = data.options.ToArray();
            float lineheight = RenderMngr.textSize("p", this.fontID).Y;

            //Centered left-right, top justified...
            Vector2 topline = screenSize * new Vector2((1 - textWidth) / 2, (1 - textHeight));

            string temp = "";
            moreBelow = 1 == clipText(data.text, ref temp, screenSize.X * textWidth, screenSize.Y * textHeight, 0);
            this.text = temp;
            this.textPos = topline;

            for (int i = 0; i < choices.Length; i++ ) {
                Choice.Option o = choices[i];

                Renderable r = new Renderable();
                r.textDepth = .5f;
                r.text = removeUnderlines(o.text) + "\r\n";
                r.textPos = topline + new Vector2(0, (i + 1) * lineheight);
                this.children.Add(r);
            }

            selector = new Renderable();
            selector.tint = Color.FromNonPremultiplied(0, 0, 255, 128);
            selector.renderType = RenderType.NonIsoBitmap;
            selector.textureID = 1;
            selector.textureDepth = 0.4f;
            selector.textureSize = new Rectangle((int)this.children[0].textPos.X, (int)this.children[0].textPos.Y,
                (int)(screenSize.X - this.children[0].textPos.X * 2), (int)lineheight);
            this.children.Add(selector);
        }
        public void updateSelection(Vector2 leftStick) {

            if (leftStick.Y > 0) index--;
            if (leftStick.Y < 0) index++;

            if (index >= choices.Length) index = 0;
            if (index < 0) index = choices.Length - 1;
            
            selector.textureSize.X = (int)this.children[index].textPos.X;
            selector.textureSize.Y = (int)this.children[index].textPos.Y;
        }
        public void updateText(Vector2 leftStick) {

            if (leftStick.Y > 0) currentLine--;
            if (leftStick.Y < 0 && moreBelow) currentLine++;
            if (currentLine < 0) currentLine = 0;

            string temp = "";
            moreBelow = 1 == clipText(data.text, ref temp, screenSize.X * textWidth, screenSize.Y * textHeight, currentLine);
            this.text = temp;

            /*
            selector.textureSize.X = (int)this.children[index].textPos.X;
            selector.textureSize.Y = (int)this.children[index].textPos.Y;
            */
        }
        public string getSelected() {
            return choices[index].label;
        }
        private string removeUnderlines(string input) {
            string output = "";
            output = input.Replace("_", "");
            output = input.Replace("/", "");
            return output;
        }
    }
}
